
#import "HSFInputFieldView.h"
#import "HSFTextView.h"
#import "MYHeader.h"

#define kContentLimit 100

@interface HSFInputFieldView()<UITextViewDelegate>
@property(nonatomic, strong) UIView *containView;
/// 输入view
@property(nonatomic, strong) HSFTextView *textView;
/// 剩余字符提示 label
@property(nonatomic, strong) UILabel *countLabel;
@end


@implementation HSFInputFieldView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.containView];
        [self.containView addSubview:self.textView];
        [self.containView addSubview:self.countLabel];
        
        [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, Space_big, 0, Space_big));
        }];
        
        [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.containView).insets(UIEdgeInsetsMake(0, 0, 5, 0));
        }];
        
        [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(self.containView);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(20);
        }];
        
        self.textView.placeholder = @"请填写办理失败原因";
        self.isInterceptHit = YES;
//        [self test];
    }
    return self;
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    /// 高度取整，防止 高清屏 出现黑线
}

- (BOOL)resignFirstResponder{
    [self.textView resignFirstResponder];
    return [super resignFirstResponder];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = self;
    [self.textView becomeFirstResponder];
    if (self.isInterceptHit) {
        view = [super hitTest:point withEvent:event];
    }
    return view;
}

//MARK: - test

- (void)test
{
    self.backgroundColor = [UIColor systemGray2Color];
}

#pragma mark - UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    self.maxInputLenth = self.maxInputLenth <= 0 ? kContentLimit : self.maxInputLenth;
   
    // textView.text 计算 unicode 字符长度 考虑 汉字与表情符号 👴🏻👮🏽
     NSInteger unicodeLenth = [textView.text unicodeLength];
    
    //textView.text 的原始长度
    static NSInteger originLenth = 0;
    if (unicodeLenth <= self.maxInputLenth) {
        originLenth = textView.text.length;
    } else {
        //截断长度限制以后的字符 避免截断字符
        NSString *tempStr = [textView.text substringWithRange:NSMakeRange(0,originLenth)];
        textView.text = tempStr;
    }
    
    NSInteger curentLenth = unicodeLenth;
    if (curentLenth > self.maxInputLenth) {
        curentLenth = self.maxInputLenth;
    }
    self.countLabel.text = [NSString stringWithFormat:@" %lu/%ld ",curentLenth, (long)self.maxInputLenth];
    
    if (self.textChange) {
        self.textChange(self.textView.text);
    }
}

#pragma mark - public methods
- (void)clearInputText{
    self.textView.text = @"";
    self.countLabel.text = [NSString stringWithFormat:@" 0/%ld ",self.maxInputLenth];
}
#pragma mark - setter getter
- (void)setPlaceholder:(NSString *)placeholder{
    if (placeholder) {
        self.textView.placeholder = placeholder;
    }
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor{
    if (placeholderColor) {
        self.textView.placeholderColor = placeholderColor;
    }
}

- (void)setTextColor:(UIColor *)textColor {
    if (textColor) {
        self.textView.textColor = textColor;
    }
}

- (void)setFont:(UIFont *)font {
    if (font) {
        self.textView.font = font;
    }
}

- (void)setCountColor:(UIColor *)countColor {
    if (countColor) {
        self.countLabel.textColor = countColor;
    }
}

- (void)setCountFont:(UIFont *)countFont {
    if (countFont) {
        self.countLabel.font = countFont;
    }
}

- (void)setMaxInputLenth:(NSInteger)maxInputLenth{
    if (maxInputLenth > 0) {
        _maxInputLenth = maxInputLenth;
        self.countLabel.text = [NSString stringWithFormat:@" 0/%ld ",_maxInputLenth];
    }
}

- (HSFTextView *)textView{
    if (!_textView) {
        _textView = [HSFTextView new];
        _textView.delegate = self;
        _textView.placeholder = @"请输入...";
        _textView.placeholderColor = [UIColor grayColor];
        _textView.textColor =  [UIColor redColor];;
        _textView.contentInset = UIEdgeInsetsMake(0, -4, 0, 0);
    }
    return _textView;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.backgroundColor = [UIColor whiteColor];
        _countLabel.textAlignment = NSTextAlignmentRight;
        _countLabel.font = [UIFont systemFontOfSize:14.0];
        _countLabel.textColor = [UIColor orangeColor];
    }
    return _countLabel;
}

- (UIView *)containView
{
    if(!_containView){
        _containView = [UIView new];
    }
    return _containView;
}

- (NSString *)text{
    return self.textView.text;
}

- (void)setText:(NSString *)text{
    self.textView.text = text;
    [self textViewDidChange:self.textView];
}

- (void)setCursorColor:(UIColor *)cursorColor{
    self.textView.tintColor = cursorColor;
}

- (void)setTextViewBackgroundColor:(UIColor *)textViewBackgroundColor {
    self.textView.backgroundColor = textViewBackgroundColor;
}

- (void)setCountLabelBackgroundColor:(UIColor *)countLabelBackgroundColor {
    self.countLabel.backgroundColor = countLabelBackgroundColor;
}
@end
